package com.ke.jdyd.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ke.jdyd.R;
import com.ke.jdyd.presenter.LoginPresenter;
import com.ke.jdyd.service.LoginService;

import java.util.HashMap;

public class LoginActivity extends AppCompatActivity implements LoginPresenter.LoginView {
  private EditText mLoginUser;
  private EditText mLoginPws;
  private ImageView mLoginUserClose;
  private ImageView mLoginPwsClose;
  private Button mLoginBnt;
  private LoginPresenter mLoginPresenter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mLoginPresenter = new LoginPresenter(this);
    mLoginUser = findViewById(R.id.login_user_name);
    mLoginPws = findViewById(R.id.login_psw_num);
    mLoginUserClose = findViewById(R.id.login_user_close);
    mLoginPwsClose = findViewById(R.id.login_pws_close);
    mLoginBnt = findViewById(R.id.login_bnt);
    mLoginBnt.setEnabled(false);
    mLoginUser.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (charSequence.length() > 0) {
          mLoginUserClose.setVisibility(View.VISIBLE);
          if (mLoginPws.getText().length() > 0) {
            mLoginBnt.setEnabled(true);
            mLoginBnt.setTextColor(getResources().getColor(R.color.white));
          }
        } else {
          mLoginUserClose.setVisibility(View.INVISIBLE);
          if (mLoginBnt.isEnabled()) {
            mLoginBnt.setEnabled(false);
            mLoginBnt.setTextColor(getResources().getColor(R.color.color_999999));
          }
        }
      }

      @Override
      public void afterTextChanged(Editable editable) {
      }
    });
    mLoginPws.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (charSequence.length() > 0) {
          mLoginPwsClose.setVisibility(View.VISIBLE);
          if (mLoginUser.getText().length() > 0) {
            mLoginBnt.setEnabled(true);
            mLoginBnt.setTextColor(getResources().getColor(R.color.white));
          }
        } else {
          mLoginPwsClose.setVisibility(View.INVISIBLE);
          if (mLoginBnt.isEnabled()) {
            mLoginBnt.setEnabled(false);
            mLoginBnt.setTextColor(getResources().getColor(R.color.color_999999));
          }
        }
      }

      @Override
      public void afterTextChanged(Editable editable) {
      }
    });
    mLoginBnt.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //调用Presenter 提交数据
        mLoginPresenter.login(mLoginUser.getText().toString(), mLoginPws.getText().toString());
      }
    });
  }

  @Override
  public void loginSuccess(LoginService.UserInfo result) {//请求成功后处理数据
    String token = result.getToken();
    if (!TextUtils.isEmpty(token)) {
      Toast.makeText(this, "登录成功~", Toast.LENGTH_SHORT).show();
      SharedPreferences sharedPreferences = getSharedPreferences("jdyd", MODE_PRIVATE);
      SharedPreferences.Editor edit = sharedPreferences.edit();
      edit.putString("user_token", token);
      edit.apply();
      finish();//标识结束当前页面 A - > B ,返回不需要A，跳转的时候直接销毁
      startActivity(new Intent(this, MainActivity.class));
    } else {
      Toast.makeText(this, "登录异常~", Toast.LENGTH_SHORT).show();
    }

  }

  @Override
  public void loginFailed(String msg) {// 请求失败的处理
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  }
}
